﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using LoongCAD.Geometry.Friendly;
using LoongCAD.STC.Friendly.Plugins;
using LoongCAD.STC.Friendly.Scene;

namespace HelloComponents
{
    /// <summary>
    /// 节点示例 (1个主零件，1+个次零件的组件)
    /// 组件项目需作为一个独立目录放置于{模型目录}/config/comps下
    /// 主程序DLL必须命名为plugin.dll, 单个plugin.dll可同时包含多个组件
    /// TestModel/config/comps/HelloComponents/thumbnail_1002.png: 组件缩略图，128x128, 用于组件管理器中图标模式下显示
    /// TestModel/config/comps/HelloComponents/icon_1002.png: 组件工具栏图标(正常状态), 24x24
    /// TestModel/config/comps/HelloComponents/icon_1002_revs.png: 组件工具栏图标(选中状态), 24x24
    /// </summary>
    [Plugin("HelloConnection")] //节点名称
    [PluginAuthor("LoongCAD")] //作者
    [PluginNumber(1002)] //节点编号
    [PluginUserInterface("HelloConnectionForm")] //节点UI界面
    [SecondaryType(SecondaryType.SECONDARYTYPE_ONE)] //节点次零件数量
    [PluginGroup("LoongSTC Examples")] //节点分组
    [PluginThumbnailLocation("thumbnail_1002", "")] //节点缩略图文件名称
    [PluginIconLocation("icon_1002", "")] //节点工具栏图标文件名称
    public class HelloConnection : LoongCAD.STC.Friendly.Plugins.ConnectionPlugin
    {
        /// <summary>
        /// 输入参数数据集合
        /// </summary>
        private HelloConnectionData m_data = null;

        /// <summary>
        /// 构造函数
        /// </summary>
        /// <param name="data">本次组件运行的输入参数</param>
        public HelloConnection(HelloConnectionData data) {
            m_data = data;
        }

        /// <summary>
        /// 运行组件
        /// </summary>
        /// <param name="comp">组件实例对象</param>
        /// <returns>本次组件运行是否成功</returns>
        public override bool Run(Connection comp) {
            var main = comp.PrimaryObject; //主零件
            var sub = comp.SecondaryObjects[0]; //次零件
            var scene = comp.Scene; //当前模型场景

            //获取组件以及主次零件的局部坐标系
            var compCoord = comp.GetCoordinateSystem();
            var mainCoord = main.GetCoordinateSystem();
            var subCoord = sub.GetCoordinateSystem();

            //获取主次零件中心线
            var mainCenterLine = main.GetCenterLine(false);
            var subCenterLine = sub.GetCenterLine(false);

            var mainLine = new LineSegment(mainCenterLine[0], mainCenterLine[1]);
            var subLine = new LineSegment(subCenterLine[0], subCenterLine[1]);
            mainLine.Intersect(subLine, out Point mainOrigin, out Point subOrigin);

            var mainDir = mainCoord.AxisY.Along(compCoord.AxisX);
            main.GetReportProperty(PartReportProperties.PROFILE_HEIGHT, out double mainPrfHeight);
            var mainFaceOrigin = mainOrigin + mainDir * mainPrfHeight * 0.5;
            var mainFace = new Plane(mainFaceOrigin, mainDir);

            //创建对齐面
            var fitting = new Fitting();
            fitting.CutTarget = sub;
            fitting.Plane = mainFace;
            fitting.Insert();

            //创建连接板
            mainFace.Intersect(subLine, out Point subEnd);
            var subDir = subCoord.AxisZ.Along(compCoord.AxisX);
            var subUp = subCoord.AxisY.Along(compCoord.AxisZ);
            var subLeft = subDir.Cross(subUp);

            sub.GetReportProperty(PartReportProperties.PROFILE_HEIGHT, out double subPrfHeight);
            sub.GetReportProperty(PartReportProperties.PROFILE_FLANGE_THICKNESS1, out double subPrfFlangeThickness1);
            sub.GetReportProperty(PartReportProperties.PROFILE_FLANGE_THICKNESS2, out double subPrfFlangeThickness2);
            sub.GetReportProperty(PartReportProperties.PROFILE_WEB_THICKNESS, out double subPrfWebThickness);
            var subPrfWebHeight = subPrfHeight - subPrfFlangeThickness1 - subPrfFlangeThickness2;

            var platePt1 = subEnd + subUp * subPrfHeight * 0.5 - subUp * subPrfFlangeThickness1
                            + subLeft * subPrfWebThickness * 0.5;
            var platePt2 = platePt1 - subUp * subPrfWebHeight;
            var platePt3 = platePt2 + subDir * m_data.PlateWidth;
            var platePt4 = platePt1 + subDir * m_data.PlateWidth;

            var plate = new ContourPlate();
            plate.AddContourPoint(new ContourPoint(platePt1, new Chamfer()));
            plate.AddContourPoint(new ContourPoint(platePt2, new Chamfer()));
            plate.AddContourPoint(new ContourPoint(platePt3, new Chamfer()));
            plate.AddContourPoint(new ContourPoint(platePt4, new Chamfer()));
            plate.Position.Depth = Position.DepthEnum.FRONT;
            plate.Position.DepthOffset = 0;
            plate.Class = "30";
            plate.Profile.ProfileString = "PL" + m_data.PlateThickness;
            plate.Insert();

            //创建焊缝
            var weld = new PolygonWeld();
            weld.Polygon.Points.Add(platePt1);
            weld.Polygon.Points.Add(platePt2);
            weld.ShopWeld = true;
            weld.ConnectAssemblies = true;
            weld.MainObject = main;
            weld.SecondaryObject = plate;
            weld.Insert();

            //创建螺栓组
            var bolts = new BoltArray();
            bolts.PartToBoltTo = plate;
            bolts.PartToBeBolted = sub;
            bolts.BoltStandard = "A";
            bolts.BoltSize = 16;
            bolts.BoltType = BoltGroup.BoltTypeEnum.BOLT_TYPE_SITE;
            bolts.Washer2 = true;
            bolts.Nut1 = true;
            bolts.ArrayDistX.Add(60.0);
            bolts.ArrayDistX.Add(60.0);
            bolts.ArrayDistY.Add(100.0);

            scene.SetCurrentTransformationPlane(new TransformationPlane());
            scene.SetCurrentTransformationPlane(new TransformationPlane(platePt1, subDir, subUp));

            bolts.FirstPosition = scene.TransformationPlane.ToLocal((platePt1 + platePt2) * 0.5);
            bolts.SecondPosition = scene.TransformationPlane.ToLocal((platePt3 + platePt4) * 0.5);
            bolts.Position.Rotation = Position.RotationEnum.TOP;
            bolts.StartPointOffset = new Vector(100.0, 0, 0);
            bolts.Insert();

            return true;
        }


    }
}
